//
//  SLHomeListCell.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/12.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit

class SLHomeListCell: UITableViewCell {

    // MARK: - UI Elements
    let backView :UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 10
        return v
    }()
    
    let icon:UIImageView = {
        let v:UIImageView = UIImageView()
        v.contentMode = .scaleToFill
        v.clipsToBounds = true
        return v
    }()
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.text = "SulitLoan"
        label.font = SLFontConfig.medium16
        label.textColor = .black
        return label
    }()
    
    let amountLabel: UILabel = {
        let label = UILabel()
        label.text = "₱ 10.000"
        label.font = UIFont.boldSystemFont(ofSize: 20)
        label.textColor = .black
        return label
    }()
    
    let rateLabel: UILabel = {
        let label = UILabel()
        label.text = "(0.05%/days)"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = .lightGray
        return label
    }()
    
    let separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = .lightGray.withAlphaComponent(0.5)
        return view
    }()
    
    let applyButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Apply", for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = rgba(hex: "#1608EC")
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 15
        button.isUserInteractionEnabled = false
        return button
    }()
    let jbImageV:UIImageView = {
        let v:UIImageView = UIImageView()
        v.contentMode = .scaleToFill
        v.image = IMAGE("icon_天数角标")
        v.clipsToBounds = true
        return v
    }()
    let dayLabel:UILabel = {
        let v:UILabel = UILabel()
        v.textColor = .white
        v.font = SLFontConfig.bold(size: 9)
        return v
    }()
    // MARK: - Initialization
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI Setup (Using SnapKit)
    private func setupUI() {
        contentView.addSubview(backView)
        backView.addSubview(icon)
        backView.addSubview(titleLabel)
        backView.addSubview(amountLabel)
        backView.addSubview(rateLabel)
        backView.addSubview(separatorView)
        backView.addSubview(applyButton)
        backView.addSubview(jbImageV)
        backView.addSubview(dayLabel)
        let rotation = CGFloat.pi/4
        let transform = CGAffineTransform(rotationAngle: rotation)
        dayLabel.transform = transform
        dayLabel.text = "91days"
        // SnapKit Constraints
        backView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.bottom.equalTo(0)
        }
        icon.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(52)
        }
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(icon.snp.right).offset(10)
            make.top.equalToSuperview().offset(20)
            
        }
        
        amountLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.equalTo(self.icon.snp.right).offset(10)
        }
        
        rateLabel.snp.makeConstraints { make in
            make.centerY.equalTo(amountLabel)
            make.left.equalTo(amountLabel.snp.right).offset(8)
        }
        
        
        
        applyButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-8)
            make.width.equalTo(82)
            make.height.equalTo(31)
        }
        jbImageV.snp.makeConstraints { make in
            make.right.top.equalTo(0)
            make.width.equalTo(40)
            make.height.equalTo(44)
        }
        dayLabel.snp.makeConstraints { make in
            make.centerX.equalTo(jbImageV).offset(5)
            make.centerY.equalTo(jbImageV).offset(-5)
        }
        self.backgroundColor = .clear
        self.selectionStyle = .none
    }
    
    // MARK: - Configuration
    func configure(with loan: Loan) {
        amountLabel.text = "₱ \(loan.amount)"
        rateLabel.text = "(\(loan.dailyRate)%/days)"
    }
}

// Example model
struct Loan {
    let amount: String
    let dailyRate: String
}


